<?php

namespace app\bis\controller;

use think\Controller;

class Login extends Controller
{
    public function index()
    {
        if (request()->isPost()) {
            //登入
            $data = input('post.');
            $ret = model('BisAccount')->get(['username'=>$data['username']]);
            if(empty($ret) || $ret->status != 1){
                $this->error('该用户不存在或者未审核通过');
            }
            $pass = $data['password'];
            $ran = $ret['code'];
            if(md5($pass."".$ran) != $ret['password']){
                $this->error('密码错误');
            }
            model('BisAccount')->updateById(['last_login_time'=>time()],$ret->id);
            //session保存用户信息 赋值bis作用域
            session('BisAccount', $ret, 'bis');
            return $this->success('登录成功',url('Index/index'));
        } else {
            $account = session('BisAccount','','bis');
            if(!empty($account) && $account->id){
                return $this->redirect(url('index/index'));
            }
            return $this->fetch();
        }
    }

    public function logout(){
        session( null,'bis');
        $this->redirect(url('login/index'));
    }
}